package com.lorenzogao.lib_view.ext

import android.content.Context
import android.util.TypedValue
import android.view.WindowManager
import android.app.Activity
import android.view.Display
import android.R.attr.x
import android.graphics.Point


/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-08 15:56
 *  description:
 */


fun Int.sp2px(context: Context): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.toFloat(), context.resources.displayMetrics)
        .toInt()
}


fun getDisplay(context: Context): Display? {
    val wm: WindowManager? = if (context is Activity) {
        context.windowManager
    } else {
        context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }
    return wm?.defaultDisplay
}


fun Int.px2dp(context: Context): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), context.resources.displayMetrics)
        .toInt()

}

fun getScreenWidth(context: Context): Int {
    val display = getDisplay(context) ?: return 0
    val point = Point()
    display.getSize(point)
    return point.x
}

fun getScreenHeight(context: Context): Int {
    val display = getDisplay(context) ?: return 0
    val point = Point()
    display.getSize(point)
    return point.y
}